var x_mark=null;
var y_mark=null;
var z_mark=null;
var clientX=null;
var clientY=null;
//**************************基本地图操作************************
function pan_Method()
{	
	var panAction = new SuperMap.Web.UI.Action3Ds.Pan(sceneControl);
	sceneControl.set_sceneAction(panAction); 	
	//sceneControl.removeEvent("objectSelected", handler);	
}
function removeCallBack()
{
	sceneControl.removeEvent("objectSelected", handler);
}

function showHouseInfo()
{
	var selAction = new SuperMap.Web.UI.Action3Ds.Select(sceneControl);
	sceneControl.set_sceneAction(selAction); 	
	sceneControl.addEvent("objectSelected", handler);	
}
//****************************飞行管理操作*************************
function flyByRoute()
{   
    sceneControl = new SuperMap.Web.UI.Controls.SceneControl(document.getElementById("sceneControlDiv"));
    scene = sceneControl.get_scene();    
    var flyManager = scene.get_flyManager();     
    var flyRoutes = flyManager.get_routes();    
	flyRoutes.fromFile(serverAddress+"Routes/FlyAround.fpf");        
    var flyRoute = flyRoutes.get_currentRoute();       
    flyRoute.set_isLinesVisible(false);    
    flyManager.play();    
}
function flyInside()
{   
    sceneControl = new SuperMap.Web.UI.Controls.SceneControl(document.getElementById("sceneControlDiv"));
    scene = sceneControl.get_scene();    
    var flyManager = scene.get_flyManager();     
    var flyRoutes = flyManager.get_routes();
    //flyRoutes.fromFile("http://localhost:8090"+"/Routes/Route01.fpf");   
	flyRoutes.fromFile(serverAddress+"Routes/FlyInside.fpf");        
    var flyRoute = flyRoutes.get_currentRoute();       
    flyRoute.set_isLinesVisible(false);    
    flyManager.play();    
}

function stopFly()
{
	var flyManager = scene.get_flyManager(); 
	flyManager.stop();		
	//var flyStatus=flyManager.get_flyStatus();	
	scene.refresh();
}    

function pauseFly()
{
	var flyManager = scene.get_flyManager(); 
	flyManager.pause();
}
function StartFly()
{
	var flyManager = scene.get_flyManager(); 
	flyManager.play(); 	
}
 function TurnLeft()
 {       
     var sceneCamera = scene.get_camera();      
     var direction=sceneCamera.get_heading();
     direction=direction-20;
     sceneCamera.set_heading(direction);
     scene.get_flyingOperator().flyTo(sceneCamera, 1500, SuperMap.Web.Realspace.FlyingMode.JUMP);
 }
function TurnRight()
 {       
     var sceneCamera = scene.get_camera();      
     var direction=sceneCamera.get_heading();
     direction=direction+20;
     sceneCamera.set_heading(direction);
     scene.get_flyingOperator().flyTo(sceneCamera, 1500, SuperMap.Web.Realspace.FlyingMode.JUMP);
 }
 function Mark()
 {
	 sceneControl.removeEvent("objectSelected", handler);
	 getPoint3D();	
 }
 //************************点的标注功能************************
 //扩展Action的构造函数
SuperMap.Web.UI.Action3Ds.DbClickAction = function(sceneControl)
{
	SuperMap.Web.UI.Action3Ds.DbClickAction.initializeBase(this);
	this._name = "myAction";
	this._sceneControl = sceneControl;
	//对应的场景操作类型为漫游
	this._type = SuperMap.Web.UI.Action3Ds.SceneActionType.POINTSELECT;
};

//第二步：设置类的属性方
SuperMap.Web.UI.Action3Ds.DbClickAction.prototype =
{  /*
	* 析构方法
	*/
	dispose:function()
	{
		this._sceneControl = null;
	},	
	
	onMouseDown:function(e)
	{
		//通过事件对象查询到相关的信息
		//alert("经度:"+e.get_longitude()+ "\n" +"纬度:"+e.get_latitude()+"\n"+"高度:"+e.get_altitude());
		//在这里添加Mark的方法，同时可以获取相应的三维坐标
		//LocationMark(e.get_longitude(),e.get_latitude(),e.get_altitude(),"haha");
		x_mark=e.get_longitude();
		y_mark=e.get_latitude();
		z_mark=e.get_altitude();
		jQuery(window.parent.document).find("#placeMark").show("slow");			
	}
	
};
//第三步：注册该类
SuperMap.Web.UI.Action3Ds.DbClickAction.registerClass('SuperMap.Web.UI.Action3Ds.DbClickAction',SuperMap.Web.UI.Action3Ds.SceneAction,Sys.IDisposable);


function getPoint3D()
	{
		//设置控件的当前操作为自定义操作
		var myAction = new SuperMap.Web.UI.Action3Ds.DbClickAction(sceneControl);
		sceneControl.set_sceneAction(myAction); 		
	} 
 //*************************获取POI点的信息**************************************
 SuperMap.Web.UI.Action3Ds.selMarkAction = function(sceneControl)
{
	SuperMap.Web.UI.Action3Ds.selMarkAction.initializeBase(this);
	this._name = "myAction";
	this._sceneControl = sceneControl;
	//对应的场景操作类型为漫游
	this._type = SuperMap.Web.UI.Action3Ds.SceneActionType.POINTSELECT;
};

//第二步：设置类的属性方
SuperMap.Web.UI.Action3Ds.selMarkAction.prototype =
{  
	dispose:function()
	{
		this._sceneControl = null;
	},	
	
	onMouseDown:function(e)
	{	
		clientX=e.get_clientX();
		clientY=e.get_clientY();
		var trackingLayer = scene.get_trackingLayer3D();	
		var feature3DMark=trackingLayer.hitTest(new SuperMap.Web.Core.Point(clientX,clientY));
		//alert(feature3DMark.get_name());
		var PoiID=feature3DMark.get_name();	
		var index=trackingLayer.indexOf(PoiID);
		//feature3DMark.set_name("HHHHHHHH");
		//var geometry1=feature3DMark.get_geometry();
				
		
		//创建三维线
		//var point3d1=new SuperMap.Web.Core.Point3D(119.345316,26.051567,100);
		jQuery.getJSON(ServletAddress+dirAddress+"id="+PoiID,function(result){
		//alert(ServletAddress+dirAddress+"id="+PoiID);
			if(result!=null)
			{
				var xx=null;
				var yy=null;
				var id=result._id;
				var route=result.route;
				var distance=result.distance
				//alert(distance);
				var len=route.length;
				var point3ds=new SuperMap.Web.Core.Point3Ds();
				for(var i=0;i<len;i++)
				{
					var x=route[i].end_location.lng;
					var y=route[i].end_location.lat;
					var point3d=new SuperMap.Web.Core.Point3D(x,y,100);
					point3ds.add(point3d);	
					//获取线中间点的位置
					if(i==Math.floor(len/2))
					{					
						xx=x;
						yy=y;						
					}					
				
				}
				var point3dsArray=new Array();
				point3dsArray.push(point3ds);
				var line3d=new SuperMap.Web.Core.GeoLine3D(point3dsArray);				
				drawLine(line3d);	
				//获取被点选的标记，然后将其重命名“全程距离”
				//trackingLayer.removeAt(index);				
				//poi(xx,yy,100,"全程距离："+distance+" 米",PoiID);	
				lineMark(xx,yy,100,"全程距离："+distance+" 米");					
			}
			else
			{
				alert("系统暂时不提供到该标记点的路径！");
			}
		
		});
		
	}
	
};
//第三步：注册该类
SuperMap.Web.UI.Action3Ds.selMarkAction.registerClass('SuperMap.Web.UI.Action3Ds.selMarkAction',SuperMap.Web.UI.Action3Ds.SceneAction,Sys.IDisposable);

 function clearMarks()
 { 
	var trackingLayer = scene.get_trackingLayer3D();
	var markCount=trackingLayer.get_count();
	//alert(markCount);
	if(markCount>0)
	{
		trackingLayer.removeAll();
	}	
 } 
 
function selectMarkEvent()
{
	var myAction = new SuperMap.Web.UI.Action3Ds.selMarkAction(sceneControl);
	sceneControl.set_sceneAction(myAction); 	
}


//****************************普通点标记和POI点标记**************************
 function LocationMark(x,y,h,Text)
 {	
	//实例化一个GeoPoint3D，然后构成Feature3D
	 var geometry = new SuperMap.Web.Core.GeoPoint3D(new SuperMap.Web.Core.Point3D(x, y, h));
	 var feature3D = new SuperMap.Web.Core.Feature3D();
	
	 var style = new SuperMap.Web.Core.Style3D();	
	 var pictureurl =serverAddress+ "images/Mark.png";		
	 style.set_markerFile(pictureurl);
	 
	 feature3D.set_style3D(style);
	 feature3D.set_geometry(geometry);
	 //创建GeoplaceMark
	  var geoPlacemark = new SuperMap.Web.Core.GeoPlacemark();
	  geoPlacemark.set_feature3D(feature3D);
	  geoPlacemark.set_name(Text);	 
	 
	 //创建几何对象为Geoplacemark的feature3D，并加入跟踪图层中
	var placemarkfeature3D = new SuperMap.Web.Core.Feature3D();
	placemarkfeature3D.set_geometry(geoPlacemark);
	
	var trackingLayer = scene.get_trackingLayer3D();
	 trackingLayer.set_isVisible(true);
	 trackingLayer.add(placemarkfeature3D, "hello");  	 
}

function poi(x,y,h,Text,PoiID)
{
	 var geometry = new SuperMap.Web.Core.GeoPoint3D(new SuperMap.Web.Core.Point3D(x, y, h));
	 var feature3D = new SuperMap.Web.Core.Feature3D();
	 //Feature3D由GeoPoint3D和Style（样式）一起构成
	 var style = new SuperMap.Web.Core.Style3D();	
	 var pictureurl =serverAddress+ "images/mark4.png";		
	 style.set_markerFile(pictureurl);
	 
	 feature3D.set_style3D(style);
	 feature3D.set_geometry(geometry);
	 //创建GeoplaceMark
	  var geoPlacemark = new SuperMap.Web.Core.GeoPlacemark();
	  geoPlacemark.set_feature3D(feature3D);
	  geoPlacemark.set_name(Text);	 
	 
	 //创建几何对象为Geoplacemark的feature3D，并加入跟踪图层中
	var placemarkfeature3D = new SuperMap.Web.Core.Feature3D();
	placemarkfeature3D.set_geometry(geoPlacemark);
	
	var trackingLayer = scene.get_trackingLayer3D();
	 trackingLayer.set_isVisible(true);
	 //这里也可以将这个Text替换成POI点的ID值，这样就可以用于路径分析了
	 trackingLayer.add(placemarkfeature3D, PoiID);  	 
}

function lineMark(x,y,h,Text)
{
 var geometry = new SuperMap.Web.Core.GeoPoint3D(new SuperMap.Web.Core.Point3D(x, y, h));
	 var feature3D = new SuperMap.Web.Core.Feature3D();		
	 feature3D.set_geometry(geometry);
	 //创建GeoplaceMark
	 var geoPlacemark = new SuperMap.Web.Core.GeoPlacemark();
	 geoPlacemark.set_feature3D(feature3D);
	 geoPlacemark.set_name(Text); 
	 //创建几何对象为Geoplacemark的feature3D，并加入跟踪图层中
	var placemarkfeature3D = new SuperMap.Web.Core.Feature3D();
	placemarkfeature3D.set_geometry(geoPlacemark);
	
	var trackingLayer = scene.get_trackingLayer3D();
	trackingLayer.set_isVisible(true);	
	trackingLayer.add(placemarkfeature3D, "lineMark");  	
	
}

//**************************飞行岛指定的房屋**************************
function FlyToGeometry(hid)
{
	removeSelect3D();
	addSelect3D(hid);
	viewEntire();
	var flyManager = scene.get_flyManager(); 
	var flyStatus=flyManager.get_flyStatus();
	if(flyStatus==SuperMap.Web.Realspace.FlyStatus.FSTOP)
	{
		var layer3DServicesList = sceneControl.get_layer3DServicesList();
		var bLoad = layer3DServicesList.load(sceneAddress,sceneName);
		if(bLoad)
		{
			var layer3DServiceInfo = layer3DServicesList.get_item(layerName);
			var dataName = layer3DServiceInfo.get_dataName();			
			//打开图层
			//var layer3D = scene.get_layer3Ds().add(sceneAddress, layerName, dataName, SuperMap.Web.Realspace.Layer3DType.VECTOR);
			//设置hid为选中状态
			scene.get_screenLayer3D().setSelected(hid);
			var layer3D=scene.get_layer3Ds().get_item(layerName);							
			if (layer3D != null) 
			{
				//获取图层的地理范围，并飞行到该范			
				var feature3D = layer3D.findFeature3DByID(hid); 
				if(feature3D==null)
					{
						alert("feature3D为空");
					}	
					else
					{
						geometry = feature3D.get_geometry();
						var GeoBound=geometry.get_bounds();
						scene.get_flyingOperator().flyToBounds(GeoBound,5000);	
						//scene.get_flyingOperator().flyToGeometry(geometry,5000, SuperMap.Web.Realspace.FlyingMode.MULTIPOINTFLY_NORMAL);					
					}			
			}
		}
	}
	
	
}









